package com.finger.wx.assistant.service;

import java.util.ArrayList;
import java.util.List;

import com.finger.wx.assistant.util.KLog;
import com.finger.wx.assistant.util.ToolProperties;
import com.finger.wx.assistant.vo.Constant;
import com.finger.wx.assistant.vo.InitParam;
import com.finger.wx.assistant.vo.Point;

/**
 * 超时设置http://blog.csdn.net/xmlrequest/article/details/8992029
 * 
 * @author Administrator
 *
 */
public class WeiXinServiceImpl extends BaseService implements WeiXinSevice {

	protected static final String TAG = "WeiXinServiceImpl";

	@Override
	public boolean init(InitParam param) {
		boolean ret = false;
		try {
			ToolProperties.setProp(Constant.locationFile, "lat", param.getLat());
			ToolProperties.setProp(Constant.locationFile, "lon", param.getLon());
			ToolProperties.setProp(Constant.locationFile, "lac", param.getLac());
			ToolProperties.setProp(Constant.locationFile, "cid", param.getCid());
			ToolProperties.setProp(Constant.locationFile, "mnc", param.getMnc());
			ret = true;
		} catch (Exception e) {
			ret = false;
		}
		return ret;
	}

	@Override
	public boolean login(String account, String pwd) {
		KLog.i(TAG, "微信登录,account:" + account + ",pwd:" + pwd);
		long start = System.currentTimeMillis();
		boolean ret = false;
		try {
			before();
			// 打开微信
			dao.runApp("com.tencent.mm", "com.tencent.mm.ui.account.LoginUI");
			while (!dao.getTopActivity(2).contains("LoginUI")) {
				KLog.i(TAG, "没有调到登录页面");
				dao.sleep(1000);
			}
			// 获取登录界面的元素的位置
			Point loginAccount = getLoginAccountPoint();
			Point loginPwd = getLoginPwdPoint();
			Point loginBtn = getLoginBtnPoint();
			// 账号
			dao.tap(loginAccount.getX(), loginAccount.getY());
			dao.inputTxt(account);
			// 密码
			dao.tap(loginPwd.getX(), loginPwd.getY());
			dao.inputTxt(pwd);
			// 登录
			dao.tap(loginBtn.getX(), loginBtn.getY());
			// 判断是否登录成功
			while (dao.topUIContains("正在登录")) {
				KLog.i(TAG, "微信登录中....");
				dao.sleep(1000);
			}
			ret = true;
		} catch (RuntimeException e) {
			ret = false;
			e.printStackTrace();
		}
		after();
		long end = System.currentTimeMillis();
		KLog.i(TAG, "登录微信,cost time:" + (end - start) + "ms");
		return ret;
	}

	@Override
	public boolean logout() {
		long start = System.currentTimeMillis();
		boolean ret = false;
		try {
			// 先杀死已有微信进程
			before();

			// 重新启动微信
			dao.runApp("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");

			// 判断微信是否已经启动
			while (!dao.topUIContains("通讯录") && !dao.topUIContains("发现")) {
				KLog.i(TAG, "微信启动中.....");
				dao.sleep(1000);
			}

			// 点击"我"
			Point me = getMenuMePoint();
			dao.tap(me.getX(), me.getY());

			// 点击"设置"
			Point setting = getMeSettingPoint();
			dao.tap(setting.getX(), setting.getY());

			// 点击"退出"
			Point tc = getMeSettingLogoutPoint();
			dao.tap(tc.getX(), tc.getY());

			// 点击弹出框"退出当前帐号"
			Point tcTip = getLogoutTipPoint();
			dao.tap(tcTip.getX(), tcTip.getY());

			// 点击提示框"退出"
			Point qrBtn = getLogoutConfirmTipPoint();
			dao.tap(qrBtn.getX(), qrBtn.getY());

			ret = true;
		} catch (RuntimeException e) {
			ret = false;
			e.printStackTrace();
		}
		after();
		long end = System.currentTimeMillis();
		KLog.i(TAG, "退出微信,cost time:" + (end - start) + "ms");
		return ret;
	}

	@Override
	public boolean sayHiNearByFriend(int sex, int num, String say_hi_txt) {
		KLog.i(TAG, "附近的人打招呼,sex:" + sex + ",num:" + num + ",say_hi_txt:" + say_hi_txt);
		long start = System.currentTimeMillis();
		boolean ret = false;
		try {
			// 先杀死已有微信进程
			before();
			// 重新启动微信
			dao.runApp("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
			// 判断微信是否已经启动
			while (!dao.topUIContains("通讯录") && !dao.topUIContains("发现")) {
				KLog.i(TAG, "微信启动中.....");
				dao.sleep(2000);
			}
			// 点击"发现"
			Point fx = getMenuDiscoverPoint();
			dao.tap(fx.getX(), fx.getY());
			dao.sleep(100);
			// 点击"附近的人"
			Point np = getDiscoverPeopleNearByPoint();
			dao.tap(np.getX(), np.getY());
			dao.sleep(100);
			String activty = dao.getTopActivity(2);
			if (activty.contains("NearbyFriendsIntroUI")) {// 开始查看
				// 点击"开始查看"
				Point ssBtn = getPeopleNearByLookBtnPoint();
				dao.tap(ssBtn.getX(), ssBtn.getY());
				dao.sleep(100);

				// 弹出提示框
				Point checkBox = getPeopleNearByTipCheckBoxPoint();
				Point cofirmBtn = getPeopleNearByTipConfirmBtnPoint();
				// 下次不提示
				dao.tap(checkBox.getX(), checkBox.getY());
				dao.sleep(100);
				// 确定
				dao.tap(cofirmBtn.getX(), cofirmBtn.getY());
				dao.sleep(100);
			} else if (activty.contains("NearbyFriendShowSayHiUI")) {// 查看附近的人
				// 点击"查看附近的人"
				Point snBtn = getPeopleNearByLookNearBtnPoint();
				dao.tap(snBtn.getX(), snBtn.getY());
				dao.sleep(100);
			}

			// 加载数据
			while (dao.topUIContains("正在查找附近的人")) {
				KLog.i(TAG, "附近的人查找中....");
				dao.sleep(1000);
			}

			// 点击"更多"
			Point moreBtn = getNearByUIMorePoint();
			dao.tap(moreBtn.getX(), moreBtn.getY());
			dao.sleep(100);

			switch (sex) {
			case 1:// 女
				Point sex_w = getNearByUIOnlyLookWomanPoint();
				dao.tap(sex_w.getX(), sex_w.getY());
				break;
			case 2:// 男
				Point sex_m = getNearByUIOnlyLookManPoint();
				dao.tap(sex_m.getX(), sex_m.getY());
				break;
			default:// 全部
				Point sex_all = getNearByUILookAllPoint();
				dao.tap(sex_all.getX(), sex_all.getY());
				break;
			}
			dao.sleep(100);
			// 加载数据
			while (dao.topUIContains("正在查找附近的人")) {
				KLog.i(TAG, "附近的人查找中....");
				dao.sleep(500);
			}

			int count = 0;
			do {
				try {
					dao.tap(360, 200);
					dao.sleep(200);

					// 获取详细资料页面,查找到"打招呼"按钮的位置
					int t_x = 0;
					int t_y = 0;
					List<Point> sayHiList = new ArrayList<Point>();
					sayHiList.add(new Point("打招呼", "android.widget.Button"));
					sayHiList.add(new Point("返回", "android.widget.ImageView"));
					dao.findCompontsLocation(sayHiList);

					t_x = sayHiList.get(0).getX();
					t_y = sayHiList.get(0).getY();

					// 找到"打招呼"按钮
					if (t_x > 0 && t_y > 0) {
						// 点击"打招呼"按钮
						dao.tap(t_x, t_y);
						dao.sleep(100);

						dao.inputTxt(say_hi_txt);

						// 点击发送
						Point sendBtn = dao.findCompontLocation("发送", "android.widget.TextView");
						dao.tap(sendBtn.getX(), sendBtn.getY());
						dao.sleep(500);
						count = count + 1;
					}

					// 点击返回
					int back_x = sayHiList.get(1).getX();
					int back_y = sayHiList.get(1).getY();
					dao.tap(back_x, back_y);
					dao.sleep(200);
				} catch (RuntimeException e2) {
					e2.printStackTrace();
				}

				// 滑动列表
				dao.swipe(360, 328, 360, 200, 500);
				dao.sleep(200);

			} while (count < num);

			ret = true;
		} catch (RuntimeException e) {
			ret = false;
			e.printStackTrace();
		}

		after();
		long end = System.currentTimeMillis();
		KLog.i(TAG, "加附近的人,cost time:" + (end - start) + "ms");
		return ret;
	}

	@Override
	public boolean verifyNearbySayHi() {
		return false;
	}

	@Override
	public boolean clearChats() {
		long start = System.currentTimeMillis();

		// 先杀死已有微信进程
		before();

		// 重新启动微信
		dao.runApp("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");

		// 判断微信是否已经启动
		while (!dao.topUIContains("通讯录") && !dao.topUIContains("发现")) {
			KLog.i(TAG, "微信启动中.....");
			dao.sleep(1000);
		}

		// 点击"我"
		Point me = getMenuMePoint();
		dao.tap(me.getX(), me.getY());

		// 点击"设置"
		Point me_setting = getMeSettingPoint();
		dao.tap(me_setting.getX(), me_setting.getY());

		// 点击"聊天"
		Point me_set_chat = getMeSettingChatPoint();
		dao.tap(me_set_chat.getX(), me_set_chat.getY());

		// 点击"清空聊天记录"
		Point me_s_chat_clear = getMeSettingChatClearPoint();
		dao.tap(me_s_chat_clear.getX(), me_s_chat_clear.getY());

		// 点击"清空"
		Point confirm = getClearChatsTipPoint();
		dao.tap(confirm.getX(), confirm.getY());

		after();
		long end = System.currentTimeMillis();
		KLog.i(TAG, "清空聊天记录,cost time:" + (end - start) + "ms");
		return true;
	}

	@Override
	public int getFriendsCount() {

		return 0;
	}

	@Override
	public boolean shareMoments(String text, boolean isImg) {
		KLog.i(TAG, "发朋友圈,text:" + text + ",isImg:" + isImg);
		long start = System.currentTimeMillis();
		boolean ret = false;
		try {
			// 先杀死已有微信进程
			before();

			// 重新启动微信
			dao.runApp("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");

			// 判断微信是否已经启动
			while (!dao.topUIContains("通讯录") && !dao.topUIContains("发现")) {
				KLog.i(TAG, "微信启动中.....");
				dao.sleep(1000);
			}

			// 点击"发现"
			Point me = getMenuDiscoverPoint();
			dao.tap(me.getX(), me.getY());

			// 点击"朋友圈"
			Point moments = getDiscoverMomentsPoint();
			dao.tap(moments.getX(), moments.getY());
			dao.sleep(3000);

			// 长按
			Point sns = getHeadMorePoint();
			dao.swipe(sns.getX(), sns.getY(), sns.getX(), sns.getY(), 1000);
			KLog.i(TAG, "更多按钮:" + sns.toString());

			// 填写朋友圈内容
			dao.inputTxt(text);

			// 点击"发送按钮"
			dao.tap(sns.getX(), sns.getY());

			ret = true;
		} catch (RuntimeException e) {
			ret = false;
			e.printStackTrace();
		}
		after();
		long end = System.currentTimeMillis();
		KLog.i(TAG, "发朋友圈,cost time:" + (end - start) + "ms");
		return ret;
	}

	@Override
	public boolean momentsLike(int max_like) {
		KLog.i(TAG, "朋友圈点赞,max_like:" + max_like);
		long start = System.currentTimeMillis();
		boolean ret = false;
		try {
			// 先杀死已有微信进程
			before();

			// 重新启动微信
			dao.runApp("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");

			// 判断微信是否已经启动
			while (!dao.topUIContains("通讯录") && !dao.topUIContains("发现")) {
				KLog.i(TAG, "微信启动中.....");
				dao.sleep(1000);
			}

			// 点击"发现"
			Point me = getMenuDiscoverPoint();
			dao.tap(me.getX(), me.getY());

			// 点击"朋友圈"
			Point moments = getDiscoverMomentsPoint();
			dao.tap(moments.getX(), moments.getY());
			dao.sleep(3000);

			// 点击"评论"
			int count = 0;
			do {
				try {
					Point like = dao.findCompontLocation("评论", "android.widget.ImageView");
					if (like != null) {
						dao.tap(like.getX(), like.getY());
						KLog.i(TAG, "评论:" + like.getX() + "," + like.getY());
						dao.sleep(500);
						Point addlike = dao.findCompontLocation("赞", "android.widget.TextView");
						if (addlike != null) {
							dao.tap(addlike.getX(), addlike.getY());
							KLog.i(TAG, "点赞:" + addlike.getX() + "," + addlike.getY());
							count = count + 1;
						}
					}
				} catch (RuntimeException e2) {
					e2.printStackTrace();
				}
				dao.swipe(360, 800, 360, 150);
			} while (count < max_like);

			ret = true;
		} catch (RuntimeException e) {
			ret = false;
			e.printStackTrace();
		}
		after();
		long end = System.currentTimeMillis();
		KLog.i(TAG, "微信点赞,cost time:" + (end - start) + "ms");
		return ret;
	}

	@Override
	public boolean momentsComment(int max_comment, String text) {
		return false;
	}

}
